package com.turing.usdtpay.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.entity.OrderData;
import com.turing.usdtpay.utils.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * @作者：JAY
 * @创建时间：2019/10/19 18:34
 * @描述：
 **/
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.Holder> {

    private Context mContext;
    private List<OrderData> mOrderDataList;
    private Holder holder;
    private OnClickListener listener;

    public OrderAdapter(Context context, List<OrderData> orderDataList) {
        this.mContext = context;
        this.mOrderDataList = orderDataList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_order, parent, false);
        holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, final int position) {
        if (mOrderDataList.get(position).getOrderType() == 1){
            holder.mBuyIv.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_buy));
            holder.mBuyTv.setText("买入");
            holder.mBuyTv.setTextColor(mContext.getResources().getColor(R.color.colorGreen));
            holder.mPay.setVisibility(View.VISIBLE);
            holder.mComplete.setVisibility(View.GONE);
        }else {
            holder.mBuyIv.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_sell));
            holder.mBuyTv.setText("卖出");
            holder.mBuyTv.setTextColor(mContext.getResources().getColor(R.color.colorRed));
            holder.mComplete.setVisibility(View.VISIBLE);
            holder.mPay.setVisibility(View.GONE);
        }
        if (mOrderDataList.get(position).getState() == 0){
            holder.mState.setText("等待付款");
            holder.mHandleLayout.setVisibility(View.VISIBLE);
            holder.mInfo.setVisibility(View.GONE);
            holder.mLimit.setText(mOrderDataList.get(position).getLimit());
            if (mOrderDataList.get(position).isTimeOut()){
                holder.mCancel.setVisibility(View.VISIBLE);
            }else {
                holder.mCancel.setVisibility(View.GONE);
            }
        }else if (mOrderDataList.get(position).getState() == 1){
            holder.mState.setText("等待确认");
            holder.mHandleLayout.setVisibility(View.VISIBLE);
            holder.mInfo.setVisibility(View.GONE);
            holder.mLimit.setText(mOrderDataList.get(position).getLimit());
            if (mOrderDataList.get(position).isTimeOut()){
                holder.mCancel.setVisibility(View.VISIBLE);
            }else {
                holder.mCancel.setVisibility(View.GONE);
            }
        }else if (mOrderDataList.get(position).getState() == 2){
            holder.mState.setText("交易完成");
            holder.mHandleLayout.setVisibility(View.VISIBLE);
            holder.mPay.setVisibility(View.GONE);
            holder.mComplete.setVisibility(View.GONE);
            holder.mCancel.setVisibility(View.GONE);
            holder.mInfo.setVisibility(View.VISIBLE);
            holder.mLimit.setText(mOrderDataList.get(position).getLimit());
        }else if (mOrderDataList.get(position).getState() == 3){
            holder.mState.setText("交易取消");
            holder.mHandleLayout.setVisibility(View.GONE);
            holder.mLimit.setText(mOrderDataList.get(position).getLimit());
        }

        holder.mPrice.setText(mOrderDataList.get(position).getPrice());
        holder.mCount.setText(mOrderDataList.get(position).getCount());
        holder.mTotal.setText(mOrderDataList.get(position).getTotal());
        holder.mTime.setText(mOrderDataList.get(position).getTime());
        if (mOrderDataList.get(position).getPayType() == 1){
            holder.mPayType.setImageResource(R.mipmap.icon_card);
        }else if (mOrderDataList.get(position).getPayType() == 2){
            holder.mPayType.setImageResource(R.mipmap.icon_pay_03);
        }else if (mOrderDataList.get(position).getPayType() == 3){
            holder.mPayType.setImageResource(R.mipmap.icon_pay_02);
        }
        holder.mPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onPay(mOrderDataList.get(position).getOrderId());
            }
        });
        holder.mComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onComplete(mOrderDataList.get(position).getOrderId());
            }
        });

        holder.mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onCancel(mOrderDataList.get(position).getOrderId());
            }
        });

        holder.mInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onInfo(mOrderDataList.get(position).getOrderType(),mOrderDataList.get(position).getOrderId());
            }
        });

    }

    public void updateDate(List<OrderData> list){
        mOrderDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mOrderDataList.size();
    }

    public class Holder extends RecyclerView.ViewHolder{
        private ImageView mBuyIv,mPayType;
        private TextView mBuyTv,mState,mPrice,mCount,mTotal,mTime,mLimit;
        private Button mPay,mComplete,mCancel,mInfo;
        private LinearLayout mHandleLayout;
        public Holder(View itemView) {
            super(itemView);
            mBuyIv = (ImageView) itemView.findViewById(R.id.item_buy_icon);
            mPayType = (ImageView) itemView.findViewById(R.id.item_pay_type);
            mBuyTv = (TextView) itemView.findViewById(R.id.item_buy_text);
            mState = (TextView) itemView.findViewById(R.id.item_state);
            mPrice = (TextView) itemView.findViewById(R.id.item_price);
            mCount = (TextView) itemView.findViewById(R.id.item_count);
            mTotal = (TextView) itemView.findViewById(R.id.item_total);
            mTime = (TextView) itemView.findViewById(R.id.item_time);
            mLimit = (TextView) itemView.findViewById(R.id.item_limit);
            mPay = (Button) itemView.findViewById(R.id.item_btn_pay);
            mComplete = (Button) itemView.findViewById(R.id.item_btn_complete);
            mCancel = (Button) itemView.findViewById(R.id.item_btn_cancel);
            mInfo = (Button) itemView.findViewById(R.id.item_btn_info);
            mHandleLayout = (LinearLayout) itemView.findViewById(R.id.item_handle_layout);

        }
    }

    public void setOnClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener{
        public void onPay(String order);
        public void onComplete(String order);
        public void onCancel(String order);
        public void onInfo(int type,String order);
    }
}
